package com.scalar.chiptrack.adaptor.asecl;

import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.HTML;
import javax.swing.text.html.parser.DTD;
import javax.swing.text.html.parser.DocumentParser;
import javax.swing.text.MutableAttributeSet;
import java.io.Reader;

/**
 * Created by IntelliJ IDEA.
 * User: Munugoti
 * Date: Sep 30, 2008
 * Time: 7:58:59 AM
 * To change this template use File | Settings | File Templates.
 */
public class ASECLHtmlParser  extends HTMLEditorKit.ParserCallback
{
    private String begMonth;
    private String begYear;
    private String begDay;
    private String endMonth;
    private String endYear;
    private String endDay;
    private String timestampAttribute;

    public void parse(Reader reader) throws Exception
    {
		DTD dtd = DTD.getDTD("HTML");
		DocumentParser documentParser = new DocumentParser (dtd);
        documentParser.parse (reader, this, true /*ignore CharSet*/);

	}

	public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos)
    {

        if( t == HTML.Tag.SELECT )
        {
            timestampAttribute = "";
            HTML.Attribute name = HTML.getAttributeKey("name");

            if ( name != null && a.getAttribute(name) != null&& a.getAttribute(name).toString() != null )
            {
                if ( a.getAttribute(name).toString().indexOf("endmm") != -1)
                {
                        timestampAttribute = "endMonth";
                }
                else if ( a.getAttribute(name).toString().indexOf("endyy") != -1)
                {
                        timestampAttribute = "endYear";
                }
                else if ( a.getAttribute(name).toString().indexOf("enddd") != -1)
                {
                        timestampAttribute = "endDay";
                }
                /*else if ( a.getAttribute(name).toString().indexOf("begmm") != -1)
                {
                        timestampAttribute = "begMonth";
                }
                else if ( a.getAttribute(name).toString().indexOf("begyy") != -1)
                {
                        timestampAttribute = "begYear";
                }
                else if ( a.getAttribute(name).toString().indexOf("begdd") != -1)
                {
                        timestampAttribute = "begDay";
                }*/
           }
        }

        if (t == HTML.Tag.OPTION )
        {
            HTML.Attribute selected = HTML.getAttributeKey("selected");
            if ( selected != null && a.getAttribute(selected) != null && a.getAttribute(selected).toString() != null )
            {
               if ("selected".equalsIgnoreCase( a.getAttribute(selected).toString() ) )
               {
                  HTML.Attribute value = HTML.getAttributeKey("value");
                  
                  if ( value != null && a.getAttribute(value) != null && a.getAttribute(value).toString() != null )
                  {
                      if ( "endMonth".equalsIgnoreCase( timestampAttribute ) )
                      {
                          setEndMonth( a.getAttribute(value).toString() );
                      }
                      else if ( "endYear".equalsIgnoreCase( timestampAttribute ) )
                      {
                           setEndYear( a.getAttribute(value).toString() );
                      }
                      else if ( "endDay".equalsIgnoreCase( timestampAttribute ) )
                      {
                           setEndDay( a.getAttribute(value).toString() );
                      }
                      /*else if ( "begMonth".equalsIgnoreCase( timestampAttribute ) )
                      {
                          setBegMonth( a.getAttribute(value).toString() );
                      }
                      else if ( "begYear".equalsIgnoreCase( timestampAttribute ) )
                      {
                           setBegYear( a.getAttribute(value).toString() );
                      }
                      else if ( "begDay".equalsIgnoreCase( timestampAttribute ) )
                      {
                           setBegDay( a.getAttribute(value).toString() );
                      }*/
                  }
               }
           }
       }
    }

	public void handleText (char[] data, int pos) {
		//String text = new String(data);
	}


    public String getBegMonth() {
        return begMonth;
    }

    public void setBegMonth(String begMonth) {
        this.begMonth = begMonth;
    }

    public String getBegYear() {
        return begYear;
    }

    public void setBegYear(String begYear) {
        this.begYear = begYear;
    }

    public String getBegDay() {
        return begDay;
    }

    public void setBegDay(String begDay) {
        this.begDay = begDay;
    }

    public String getEndMonth() {
        return endMonth;
    }

    public void setEndMonth(String endMonth) {
        this.endMonth = endMonth;
    }

    public String getEndYear() {
        return endYear;
    }

    public void setEndYear(String endYear) {
        this.endYear = endYear;
    }

    public String getEndDay() {
        return endDay;
    }

    public void setEndDay(String endDay) {
        this.endDay = endDay;
    }
}
